//
//  GroupView.m
//  SimpleTimeDemo
//
//  Created by lanou3g on 15/11/16.
//  Copyright © 2015年 王丽珍. All rights reserved.
//

#import "GroupView.h"
#import "GroupTableViewCell.h"
#import "GroupModel.h"
#import "MainDataHelper.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"

@interface GroupView () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *groupTableView;

@property (nonatomic, strong) UISegmentedControl *groupSegment;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, copy) NSString *string;
@property (nonatomic, copy) NSString *bodyString;

@property (nonatomic, assign) int pageNum;

@end

@implementation GroupView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.groupSegment = [[UISegmentedControl alloc] initWithItems:@[@"Hot", @"New"]];
        self.groupTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, self.frame.size.width, self.frame.size.height - 94) style:UITableViewStylePlain];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //刷新次数
    self.pageNum = 1;
    
    self.groupSegment.selectedSegmentIndex = 0;
    self.groupSegment.frame = CGRectMake(100, 0, self.frame.size.width - 2*100, 25);
    [self.groupSegment addTarget:self action:@selector(groupSegment:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:self.groupSegment];
    
    self.string = @"http://api2.pianke.me/group/group_list";
    self.bodyString = @"auth=&client=1&deviceid=4B1DC887-3B93-405F-970B-3B2C4C556E2A&limit=10&sort=hot&start=0&version=3.0.6";
    [self loadData];
    
    [self loadTableView];
    
    //刷新操作--第三方
    [self setupRefresh];
}

- (void)setupRefresh {
    self.groupTableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(pushReload)];
    self.groupTableView.footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(downReload)];
}

- (void)pushReload {
    [self groupSegment:self.groupSegment];
    [self.groupTableView.header beginRefreshing];
}

- (void)downReload {
    self.pageNum++;
    [self groupSegment:self.groupSegment];
    [self.groupTableView.footer beginRefreshing];
}

- (void)groupSegment:(UISegmentedControl *)sender {
    switch (sender.selectedSegmentIndex) {
        case 0:
            self.string = @"http://api2.pianke.me/group/group_list";
            self.bodyString = [NSString stringWithFormat:@"auth=&client=1&deviceid=4B1DC887-3B93-405F-970B-3B2C4C556E2A&limit=%d&sort=hot&start=0&version=3.0.6", self.pageNum*10];
            [self loadData];
            break;
        case 1:
            self.string = @"http://api2.pianke.me/group/group_list";
            self.bodyString = [NSString stringWithFormat:@"auth=&client=1&deviceid=4B1DC887-3B93-405F-970B-3B2C4C556E2A&limit=%d&sort=addtime&start=0&version=3.0.6", self.pageNum*10];
            [self loadData];
            break;
            
        default:
            break;
    }
}

- (void)loadData
{
    [MainDataHelper postDataWithUrlString:self.string andBodyString:self.bodyString compare:^(id object) {
        self.dataArray = [NSMutableArray array];
        
        NSDictionary *dic = object;
        NSArray *array = dic[@"data"][@"list"];
        for (NSDictionary *dic1 in array) {
            GroupModel *model = [[GroupModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [self.dataArray addObject:model];
        }
        
        [self.groupTableView reloadData];
        [self.groupTableView.header endRefreshing];
        [self.groupTableView.footer endRefreshing];
    }];
}

- (void)loadTableView {
    self.groupTableView.delegate = self;
    self.groupTableView.dataSource = self;
    [self addSubview:self.groupTableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GroupModel *model = self.dataArray[indexPath.row];
    
    [self.groupTableView registerClass:[GroupTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    GroupTableViewCell *groupCell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    groupCell.titleLabel.text = model.title;
    [groupCell.userImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    for (int i = 0; i < model.tags.count; i++) {
        NSString *typeStr = groupCell.typeLabel.text;
        groupCell.typeLabel.text = [typeStr stringByAppendingFormat:@"%@/", model.tags[i]];
    }
    groupCell.joinLabel.text = [NSString stringWithFormat:@"%ld人加入", model.membernum];
    
    [groupCell.firstButton setTitle:model.latestposts[0][@"title"] forState:UIControlStateNormal];
    groupCell.firstButton.titleLabel.numberOfLines = 3;
    [groupCell.firstButton sizeToFit];
    [groupCell.secondButton setTitle:model.latestposts[1][@"title"] forState:UIControlStateNormal];
    groupCell.secondButton.titleLabel.numberOfLines = 3;
    [groupCell.secondButton sizeToFit];
    [groupCell.thirdButton setTitle:model.latestposts[2][@"title"] forState:UIControlStateNormal];
    groupCell.thirdButton.titleLabel.numberOfLines = 3;
    [groupCell.thirdButton sizeToFit];
    [groupCell.fourButton setTitle:model.latestposts[3][@"title"] forState:UIControlStateNormal];
    groupCell.fourButton.titleLabel.numberOfLines = 3;
    [groupCell.fourButton sizeToFit];
    groupCell.nameLabel.text = [NSString stringWithFormat:@"组长:%@", model.userinfo[@"uname"]];
    groupCell.timeLabel.text = model.lastupdate_f;
    
    groupCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return groupCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    GroupModel *model = self.dataArray[indexPath.row];
    self.groupStr = model.groupid;
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"group" object:self];
}



@end
